<?php

/**
 * Created by ymk
 * User: Administrator
 * Date: 2018/6/27
 * Time: 11:51
 */

namespace MeetUp\Model;

use Validator;

class Activity extends Base
{
    protected $table = 'activity';
    protected $primaryKey = 'activity_id';
    protected $fillable = ['uid','activity_name','E','N','end_time','start_time','time_supply','activity_img','details','activity_groupid','close','is_hot'];
    //关联用户users
    public function user(){
        return $this->hasOne('MeetUp\Model\Users','id','uid');
    }
    //聚会详细信息
    public function activity($data,$uid,$url){
        $user_activity = new UserActivity();
        $activity_collect = new ActivityCollect();
        foreach ($data as $k => $v){
            //图片地址
            $data[$k]['activity_img']  = $url.$data[$k]['activity_img'];
            //参与聚会的成员
            $userinfo = $user_activity->where(['m_activityid'=>$v['activity_id']])->get()->toArray();
            foreach ($userinfo as $key => $value){
                $data[$k]['userimg'][] = UserActivity::find($value['id'])->user()->first()->thumimg;
            }
            //是否收藏
            $result = $activity_collect->where(['uid'=>$uid,'activityid'=>$v['activity_id']])->first();
            if($result){
                $data[$k]['is_collect'] = 1;
            }else{
                $data[$k]['is_collect'] = 0;
            }

            //参与聚会的人数
            //$data[$k]['user_count'] = $user_activity->user_count($v['activity_id']);
            //时间转换
            $data[$k]['start_time'] = date('n月j日 星期N',$v['start_time']);
            $data[$k]['time'] = date('H:i',$v['start_time']);
        }
        return $data;
    }

    //验证
    public function inspect($request){
        //验证数据
        $message=[
            'uid.required'=>'创建人不能为空',
            'name.required'=>'名称不能为空',
            'start_time.required'=>'开始时间不能为空',
            'end_time.required'=>'结束时间不能为空',
            'details.required'=>'描述不能为空',
            'group_id.required'=>'分组ID不能为空',
        ];
        $validator = Validator::make($request->all(), [
            'uid' => 'required',
            'name' => 'required',
            'start_time' => 'required',
            'end_time' => 'required',
            'details' => 'required',
            'group_id' => 'required',
        ],$message);
        if ($validator->fails()) {
            $data = $validator->errors()->all();
            return $data;
        }
    }
}